<?php

/**
 *	This class is a data model object for representation of address information.
 *
 */
class DrugOriginalName extends ORDataObject {

	var $drug_original_name_id = '';
	var $name		= '';
	var $duoc_luc	= '';
	var $duoc_dong_luc		= '';
	var $tac_dung		= '';
	var $chi_dinh		= '';
	var $chong_chi_dinh	= '';
	var $disable	= '';

	var $_key='drug_original_name_id';
	var $_table='drug_original_name';
	var $_internalName='DrugOriginalName';
	

	public function DrugOriginalName($db = null) {
		parent::ORDataObject($db);
	}
		
	public function setup($id = 0) {
		if ($id !== 0) {
			$this->setId($id);
			$this->populate();
		}

	}
	public function setId($id){
		$this->drug_original_name_id=$id;
	}
	public function populate(){
		parent::populate('drug_original_name_id');
	}
	public function getId(){
		return $this->id;
	}
	
    /*
     * get name and id for autocomplete
     */
	public function getOriginalNameForAutoComplete($needle) {
		$ds =& new Datasource_sql();
		$sql = array(
            'cols'  => "drug_original_name_id, name ",
            'from'  => $this->_table ,
            'where' => "disable =0 and name LIKE '%$needle%' "
		);
		
		$ds->setup($this->_db, $sql);
		return $ds->toArray();
	}
    
	/*
	 * delete a original name update disable=1
	 */
	public function updateOriginalName($Id=null) {
		$sql = "update $this->_table  set  disable= 1 where $this->_key = $Id ";
		$result = $this->_execute($sql);
		return $result;
	}
	
	/*
	 * get all field original
	 */
	public function getAllOriginalName($name=null) {
		$ds = new Datasource_sql();
		$where = "disable = 0";
		if($name!=null){
			$where = "disable = 0 and name='".trim($name)."'";
		}
		$sql = array(
			'cols' 		=> "*",
			'from' 		=> $this->_table,
			'where'		=> $where,
			'orderby' 	=> "name"
			);

		$ds->setup($this->_db, $sql);
		$result = $ds->toArray();
		return $result;

	}
	/*
	 * get name by id
	 */
    public function getNameById($id=null) {
        $ds = new Datasource_sql();
        $where = "disable = 0";
        if($id!=null){
            $where = "disable = 0 and drug_original_name_id=".$id;
        }
        $sql = array(
            'cols'      => "*",
            'from'      => $this->_table ,
            'where'     => $where,
            'orderby'   => "name",
            'limit'     =>1
            );

        $ds->setup($this->_db, $sql);
        $result = $ds->toArray();
        return $result;

    }

    /*
     * insert to db
     */
	public function savedata($array){
		$data = array (
			'drug_original_name_id'  => $array['drug_original_name_id'],
            'name'      		 => trim($array['name']),
            'duoc_luc'     		 => $array['duoc_luc'],
            'duoc_dong_luc'      => $array['duoc_dong_luc'],
            'tac_dung'     		 => $array['tac_dung'],
            'chi_dinh'			 => $array['chi_dinh'],
			'chong_chi_dinh'     => $array['chong_chi_dinh'],
		);
		$this->populateArray($data);
		$this->persist();
		return $this->persist();
	}

	/*
	 * check name not dulplicate
	 */
	public function checkNamSame($name){
		$ds = new Datasource_sql();
		$sql = array(
            'cols'    => "name",
            'from'    =>  $this->_table ,
            'where'   => "disable =0 and name ='". trim($name)."'"
		);
		$ds->setup($this->_db, $sql);
		$result = $ds->toArray('name');
		if (sizeof($result) == 0) return false;
		return true;
	}
	/*
	 * check name dul for edit
	 */
	public function checkOriginalNamEdit($name, $id){
		$ds = new Datasource_sql();
		$sql = array(
            'cols'    => "name",
            'from'    => $this->_table ,
            'where'   => "disable =0 and name ='". trim($name)."' and drug_original_name_id<>".$id
		);
		$ds->setup($this->_db, $sql);
		$result = $ds->toArray('name');
		if (sizeof($result) == 0) return false;
		return true;
	}
	/*
	 * sum with condition disable =0
	 */
	public function getTotalNumber(){
		$ds = new Datasource_sql();
		$sql = array(
            'cols'    => "count(drug_original_name_id) as total",
            'from'    =>  $this->_table ,
            'where'   => "disable =0 "
		);
		$ds->setup($this->_db, $sql);
		$result = $ds->toArray('total');
		return $result[0];
	}
	/*check $name if exit in table and return with id else return false
	 * $param $name orginal name
	 */
    public function checkExitNameReturnId($name){
        $ds = new Datasource_sql();
        $sql = array(
            'cols'    => "name, drug_original_name_id",
            'from'    =>  $this->_table ,
            'where'   => "disable =0 and name like '".trim($name)."'"
        );
        $ds->setup($this->_db, $sql);
        $result = $ds->toArray('drug_original_name_id');
        return $result[0];
    }
   
    /**
     * List all medicine original name.
     *
     * @return Array of all original name of medicine.
     */
    public function listAll() {
        $ds = new Datasource_sql();
        $sql = array (
            'cols'      => "$this->_key, name",
            'from'      => $this->_table,
            'orderby'   => "name");
        $ds->setup($this->_db, $sql);
        return $ds->toArray("$this->_key", "name");
    }
		
}
?>
